$(function () {
    var itemCatName;
    var pic = [];
    var cId = "";


    var itemCatNameObj = $("#itemCatName");


    layui.use(['form', 'upload', 'layedit'], function () {
        var form = layui.form
            , layer = layui.layer
            , upload = layui.upload
            , layedit = layui.layedit;

        layedit.set({
            uploadImage: {
                url: '/content/addPicDes' //接口url
                , type: 'post' //默认post
            }
        });
        //构建一个默认的编辑器
        var index = layedit.build('des');
        //form表单提交数据 是以name = des value = 用户输入的值
        layedit.sync(index);
        //编辑器外部操作
        var active = {
            content: function () {
                alert(layedit.getContent(index)); //获取编辑器内容
            }
            , text: function () {
                alert(layedit.getText(index)); //获取编辑器纯文本内容
            }
            , selection: function () {
                alert(layedit.getSelection(index));
            }
        };

        //只要用户点击上传图片按钮以后 选择了图片以后 就会执行这个代码
        upload.render({
            elem: '#uploadPic'
            , url: '/content/addPic' //改成您自己的上传接口
            , multiple: true
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    //往showPic（div）里面创建img标签设置他的一系列东西
                    $('#showPic').append('<img src="' + result + '" alt="' + file.name + '" class="layui-upload-img">')
                });
            }
            , done: function (res) {
                pic.push(res.data);
            }
        });


        //点击保存按钮的点击事件
        $("#saveBtn").click(function () {
            var json = {};
            if (cId == "") {
                layer.alert("请选择商品分类");
                return;
            }
            json.cId = cId;
            var inputArr = $("input[name='paramId']");
            var params = [];
            $.each(inputArr, function (i, n) {
                var j = {};
                j.paramId = $(n).attr("id");
                j.paramValue = $(n).val();
                params.push(j);
            })
            var titleVal = $("#title").val();
            var sellPointVal = $("#sellPoint").val();
            var priceVal = $("#price").val();
            var numVal = $("#num").val();
            var barcodeVal = $("#barcode").val();
            var imageVal = pic.join(",");
            var desVal = layedit.getContent(index);
            json.title = titleVal;
            json.sellPoint = sellPointVal;
            json.price = priceVal * 100;
            json.num = numVal;
            json.barcode = barcodeVal;
            json.image = imageVal;
            json.des = desVal;
            json.params = params;

            $.ajax({
                type: "POST",
                url: "/item/addItem",
                dataType: "json",
                contentType: 'application/json;charset=UTF-8',
                data: JSON.stringify(json),
                success: function (msg) {
                    if (msg.code == "ErrorResponse") {
                        layer.alert("添加商品失败请联系管理员");
                    } else {
                        layer.alert(msg.msg);
                    }
                    //自己清空分类id值
                    itemCatNameObj.text("");
                    //使用layui情况form表单input输入框的值
                    $("#addItem")[0].reset();
                    layui.form.render();
                    //自己清空 多图片上传的图片
                    $("#showPic>img").remove();
                    index = layedit.build('des');

                }
            });

        })
    });
})